import Card from "./Card";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Character extends cc.Component {
    /** 基础数据 */
    cards:{[name:string]:Card}={};
    cards2:Card[]=[];
    _character:QM.Character=null;
    
    @property(cc.Prefab)
    cardPrefab:cc.Prefab=null;

    cardParent:cc.Node;        
    nameLabel:cc.Label;
    campLabel:cc.Label;
    btnsParent:cc.Node;
    readyButton:cc.Node;
    outButton:cc.Node;    
    jumpButton:cc.Node;
    readyLabel:cc.Node;
    
    
  
    onLoad () {
        this.cardParent = this.node.getChildByName("Cards");                      
        this.readyLabel = this.node.getChildByName("readyLabel");
        this.nameLabel = this.node.getChildByName("name").getComponent(cc.Label);
        this.campLabel = this.node.getChildByName("campLabel").getComponent(cc.Label);
        this.btnsParent = this.node.getChildByName("Btns");
        this.readyButton = this.btnsParent.getChildByName("readyButton");
        this.outButton = this.btnsParent.getChildByName("outButton");
        this.jumpButton = this.btnsParent.getChildByName("jumpButton");        
        this.reset();

        QM.Socket.Instance.on(this.message,this);
           
    }

    set character(c:QM.Character){        
        this._character = c;
        this.updateUI();
        this.initEvent();
    }
    get character(){        
        return this._character;
    }

    
    /** 重置 */
    reset(){
        this.cardParent.removeAllChildren();        
        this.nameLabel.string = "";
        this.campLabel.string = "";
        this.readyLabel.active = false;
        this.btnsParent.active = false;
    }
        
 

    /** 更新角色UI显示 */
    updateUI(){               
        this.nameLabel.string = this.character.username;
        //如果房间已满，直接显示准备按钮
        this.campLabel.string = "";

        if(QM.Room.Instance.length==3){            
            this.showReadyBtn();
        }        
        
       

        if(this.character.point==QM.QPoint.Bottom){
            return;
        }
       
        var widget = this.cardParent.getComponent(cc.Widget);
        var layout = this.cardParent.getComponent(cc.Layout);            

        widget.isAlignTop = true;
        widget.top = 50;
        widget.isAlignBottom = false;

        layout.node.width=66;
        layout.type = cc.Layout.Type.VERTICAL;
        layout.spacingX = 0;
        layout.spacingY = -75;            
        var cs = layout.getComponentsInChildren(cc.Sprite);
        cs.forEach(sp=>{
            sp.node.position = cc.Vec2.ZERO;
        })
        layout.resizeMode = cc.Layout.ResizeMode.CONTAINER;


        var nameWidget = this.nameLabel.getComponent(cc.Widget);
        nameWidget.isAlignBottom = false;
        nameWidget.isAlignTop = true;
        nameWidget.top=5;

        var campWidget = this.campLabel.getComponent(cc.Widget);     
        campWidget.isAlignBottom = false;
        campWidget.isAlignHorizontalCenter = false;
        campWidget.isAlignTop = true;
        campWidget.top=5;


        var readyWidget = this.readyLabel.getComponent(cc.Widget);
        readyWidget.isAlignVerticalCenter = true;
        readyWidget.isAlignBottom = false;


        if(this.character.point==QM.QPoint.Left){            
            widget.isAlignLeft=true;
            widget.left = 30;    
            
            nameWidget.isAlignLeft = true;
            nameWidget.left = 5;

            campWidget.isAlignLeft = true;
            campWidget.left = 200;

            readyWidget.isAlignLeft = true;
            readyWidget.left = 20;
            
        }else if(this.character.point==QM.QPoint.Right){

            widget.isAlignRight=true;
            widget.right = 30;

            nameWidget.isAlignRight = true;
            nameWidget.right=5;

            campWidget.isAlignRight = true;
            campWidget.right = 200;

            readyWidget.isAlignRight = true;
            readyWidget.right = 20;

        }


        
    }

    /** 显示阵营文字 */
    showCamp(){
        if(this.character.uid==QM.Room.Instance.landLord){
            this.campLabel.string = "地主";
        }else{
            this.campLabel.string = "农民";
        }
    }

    /** 初始化监听事件 */
    initEvent(){
        if(this.character.point == QM.QPoint.Bottom){
            this.outButton.on(cc.Node.EventType.TOUCH_START,()=>{
                this.outCards()
            });
            this.readyButton.on(cc.Node.EventType.TOUCH_START,()=>{
                this.onReady()
            });
            this.jumpButton.on(cc.Node.EventType.TOUCH_START,()=>{
                this.onJump();
            },this)

        }
    }

    /** 接收牌 
     * //这条消息是单推不是群发
     * obj:{uid:number,cards:QM.Card[]|number}[]
    */
    receiveCards(obj:{uid:number,cards:QM.Card[]|number}[]){                
        this.hideReadyText();

        obj.forEach(item=>{
            if(item.uid==this.character.uid){                
                var cards:QM.Card[] = [];            
                if( typeof item.cards=="number"){
                  
                    for(var i=0;i<item.cards;i++){
                        cards.push({name:"CardBack",title:"CardBack"});
                    }                  
                
                }else{
                    cards = item.cards;                    
                }
                cards.forEach(card=>{
                    var CardNode = cc.instantiate(this.cardPrefab);
                    CardNode.parent = this.cardParent;
                    var JC = CardNode.getComponent(Card);
                    JC.card = card;
                    JC.point = this.character.point;
                    if(card.title=="CardBack"){                        
                        this.cards2.push(JC);
                    }else{                     
                        this.cards[card.title] = JC;
                    }
                    
                })
            }
        })                     
    }

    /** 接收底牌 */
    receiveBottomCards(obj:{uid:number,cards:QM.Card[]}){        
        //加入底牌
        var addCards:QM.Card[] = [];
        if(this.character.point==QM.QPoint.Bottom){
            addCards = obj.cards;
        }else{
            for(var i=0;i<obj.cards.length;i++){
                addCards.push({name:"CardBack",title:"CardBack"});
            }    
        }
        addCards.forEach(card=>{
            var CardNode = cc.instantiate(this.cardPrefab);
            CardNode.parent = this.cardParent;
            var JC = CardNode.getComponent(Card);
            JC.card = card;
            JC.point = this.character.point;
            if(card.title=="CardBack"){                        
                this.cards2.push(JC);
            }else{                     
                this.cards[card.title] = JC;                
            }            
        })     
        
        
        //对手牌进行排序
        var temp:Card[] = [];
        if(this.character.point==QM.QPoint.Bottom){
            for(let k in this.cards){
                temp.push(this.cards[k]);
            }

            temp.sort((a,b)=>{
                var s = a.value-b.value;
                if(s==0){
                    s = a.cvalue-b.cvalue;
                }
                return s;
            });
        }
        temp.forEach((card,key)=>{
            card.node.setSiblingIndex(key);
        })
        
        //地主显示出牌按钮
        if(this.character.uid==obj.uid && this.character.point==QM.QPoint.Bottom){
            this.showOutBtn(1);
        }

    }
    
    
 
    /**
     *  出牌后服务端返回值 
     *  跳过出牌
     * */    
    receiveOutCards(obj:{uid:number,cards?:QM.Card[],next?:number,new?:number}){       
        if(obj.cards){
            var outCards:Card[] = []
            if(this.character.point==QM.QPoint.Bottom && this.character.uid==obj.uid){
                //取出要出的牌    
                
                obj.cards.forEach(card=>{
                    var oc = this.cards[card.title]                
                    delete this.cards[card.title];
                    outCards.push(oc);
                })            
            }else if(this.character.uid==obj.uid){
                var length = obj.cards.length;
                outCards = this.cards2.splice( -length,length );
            }            
            if(outCards.length){
                outCards.forEach(card=>{
                    card.node.parent = null;
                })
            }
        }

        
        
        if(this.character.uid==obj.uid){
            this.hideOutBtn();
        }        
        if(obj.next && this.character.uid == obj.next){
            this.showOutBtn(obj.new);
        }
        
    }
    

    /** 出牌 */
    outCards(){ 
        var outCard:QM.Card[] = [];
        for(var k in this.cards){        
            if(this.cards[k].isSelect){
                outCard.push(this.cards[k].card)
            }
        }
        
        //发送给服务端        
        var obj={
            c:"Game",
            a:"outCards",
            d:outCard
        }  
        QM.Socket.Instance.Send(obj);
    }

    /** 准备按钮点击 */
    onReady(){
        var obj = {
            c:"Room",
            a:"ready"
        }
        QM.Socket.Instance.Send(obj);
    }

    /** 跳过 */
    onJump(){
        var obj={
            c:"Game",
            a:"jump"
        }
        QM.Socket.Instance.Send(obj);
    }


    message(ev:QM.QEvent){
        var cmd =ev.cmd;
        var data = ev.data;
        if(cmd && this[cmd]){
            this[cmd](data);
        } 
    }
   
    /** 显示准备按钮 */
    showReadyButton(){  
        this.cardParent.removeAllChildren();
        this.campLabel.string = "";
        this.showReadyBtn();        
    }



    /** 显示已准备文字 */
    showReadyText(obj:{uid:number}){                
        if(this.character.uid==obj.uid){
            this.readyLabel.active = true;
            this.readyButton.active = false;        
        }
    }

    /** 关闭已准备文字 */
    hideReadyText(){
        this.readyLabel.active = false;
    }


    /** 显示出牌按钮 */
    showOutBtn(flag?:number){
        if(flag){
            this.jumpButton.active = false;
        }else{
            this.jumpButton.active = true;            
        }
        this.outButton.active = true;        
    }
    /** 隐藏出牌按钮 */
    hideOutBtn(){
        this.outButton.active = false;
        this.jumpButton.active = false;
    }

    /** 显示准备按钮 */
    showReadyBtn(){
        if(this.character.point==QM.QPoint.Bottom){           
            this.btnsParent.active = true;
            this.readyButton.active = true;
            this.outButton.active = false;
            this.jumpButton.active = false;
        }
    }
    onDestroy(){
        QM.Socket.Instance.off(this.message,this);
    }


}
